package com.roncoo.chapter.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roncoo.chapter.entity.EduChapter;
import com.roncoo.chapter.entity.EduChapterPeriod;
import com.roncoo.chapter.service.EduChapterService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wdf
 * @since 2021-04-07
 */
@RestController
@RequestMapping("/eduChapter")
public class EduChapterController {
    @Resource
    private EduChapterService eduChapterService;
    @ApiOperation(value = "根据id查询 ")
    @PostMapping("findChapterById/{id}")
    public List<EduChapterPeriod> findChapterPeriodById(@PathVariable("id") Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("course_id",id);
        return this.eduChapterService.list(queryWrapper);
    }

    @ApiOperation(value = "新增或修改 ")
    @PostMapping("update")
    public Boolean findChapterPeriodById(EduChapter eduChapter) throws ParseException {
        System.out.println(eduChapter);
        Date date = new Date();
        SimpleDateFormat sm2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String str = sm2.format(date);
        Date parse = sm2.parse(str);
        eduChapter.setModifiedTime(parse);
        if(eduChapter.getCreateTime() == null || eduChapter.getCreateTime()== "")
            eduChapter.setCreateTime(str);
        return this.eduChapterService.saveOrUpdate(eduChapter);
    }

    @ApiOperation(value = "删除 ")
    @PostMapping("del")
    public Boolean delById(EduChapter eduChapter){
        return this.eduChapterService.removeById(eduChapter);
    }


    /**
     * 根据课程id查询所有章和节
     * @return
     */
    @GetMapping("findByCid")
    public List<EduChapter> findByCid(Integer courseId){
        return eduChapterService.getAll(courseId);
    }



}

